package io.renren.modules.sys.customer.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.customer.entity.BtCategroyManageEntity;
import io.renren.modules.sys.customer.entity.BtCustomerBaseEntity;
import io.renren.modules.sys.customer.entity.BtCustomerNavbarEntity;
import io.renren.modules.sys.customer.entity.BtNavBarEntity;
import io.renren.modules.sys.customer.service.BtCategroyManageService;
import io.renren.modules.sys.customer.service.BtCustomerBaseService;
import io.renren.modules.sys.customer.service.BtCustomerNavbarService;
import io.renren.modules.sys.customer.service.BtNavBarService;
import io.renren.modules.sys.customer.vo.BtCustomerNavbarVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author goudandan
 * @email reszj1@gmail.com
 * @date 2018-09-12 16:49:25
 */
@RestController
@RequestMapping("sys/catagroyManager")
public class BtCatagroyManagerController extends AbstractController {
    @Autowired
    private BtCustomerNavbarService btCustomerNavbarService;
    @Autowired
    private BtCustomerBaseService btCustomerBaseService;

    /**
     *获取产品分类树
     */
    @RequestMapping("/getcategoryztree")
    public R GetPCZtree(){
        Long uid = getUserId();
        if(uid.equals(0)){
            return R.error("非法用户！！");
        }
        BtCustomerBaseEntity btCustomerBase = btCustomerBaseService.SelectOneByUid(uid);
        if(btCustomerBase==null){
            return R.error("没有该店铺！");
        }
        List<BtCustomerNavbarEntity> returntree = new ArrayList<>();
        List<BtCustomerNavbarEntity> pcztree = btCustomerNavbarService.selectList(new EntityWrapper<BtCustomerNavbarEntity>().eq("cust_id",btCustomerBase.getId()).eq("is_hassub","1"));
        BtCustomerNavbarEntity dd = new BtCustomerNavbarEntity();
        dd.setId((long)(0));
        dd.setPid((long)(-1));
        dd.setName("课程分类");
        returntree.add(dd);
        for (BtCustomerNavbarEntity pbean :pcztree){
            returntree.add(pbean);
            List<BtCustomerNavbarEntity> sublist = btCustomerNavbarService.selectList(new EntityWrapper<BtCustomerNavbarEntity>().eq("cust_id",btCustomerBase.getId()).eq("pid",pbean.getId()));
            for (BtCustomerNavbarEntity subbean :sublist){
                subbean.setParentName(pbean.getName());
                returntree.add(subbean);
            }
        }
        return R.ok().put("categroyztree",returntree);
    }
    /*@RequestMapping("/addormodcategroy")
    @RequiresPermissions("sys:btnavbar:save")
    public R AddorModPC(@RequestBody NavBarCVo categvo){
        if(null != categvo.getId()){
            BtNavBarEntity modbean = btNavBarService.selectById(categvo.getId());
            if(null != modbean){
                modbean.setName(categvo.getName());
                btNavBarService.updateById(modbean);
                return R.ok();
            }else{
                return R.error("无修改的分类");
            }
        }else{
            BtNavBarEntity addbean = new BtNavBarEntity();
            addbean.setName(categvo.getName());
            addbean.setPid(categvo.getPid());
            btNavBarService.insert(addbean);
            return R.ok();
        }
    }*/

    /**
     * 删除
     */
    /*@RequestMapping("/delcategroy/{id}")
    @RequiresPermissions("sys:btnavbar:save")
    public R delProCateg(@PathVariable("id") Long id){
        btNavBarService.deleteById(id);
        return R.ok();
    }*/
    /**
     * 能否删除子分类
     */
    /*@RequestMapping("/iscandelete/{id}")
    public R IsCanDelete(@PathVariable("id") Long id){
        Integer count= btNavBarService.hadSubCount(id);
        if(count>0){
            return R.ok();
        }else{
            return R.error();
        }
    }*/


    /**
     * 列表
     */
    /*@RequestMapping("/list")
    @RequiresPermissions("sys:btnavbar:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = btNavBarService.queryPage(params);
        return R.ok().put("page", page);
    }*/


    /**
     * 信息
     */
   /* @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:btnavbar:info")
    public R info(@PathVariable("id") Long id){
        BtNavBarEntity btNavBar = btNavBarService.selectById(id);

        return R.ok().put("btNavBar", btNavBar);
    }*/

    /**
     * 保存
     */
   /* @RequestMapping("/save")
    @RequiresPermissions("sys:btnavbar:save")
    public R save(@RequestBody BtNavBarEntity btNavBar){
        btNavBarService.insert(btNavBar);

        return R.ok();
    }*/

    /**
     * 修改
     */
    /*@RequestMapping("/update")
    @RequiresPermissions("sys:btnavbar:update")
    public R update(@RequestBody BtNavBarEntity btNavBar){
        ValidatorUtils.validateEntity(btNavBar);
        btNavBarService.updateAllColumnById(btNavBar);//全部更新
        
        return R.ok();
    }*/

    /**
     * 删除
     */
    /*@RequestMapping("/delete")
    @RequiresPermissions("sys:btnavbar:delete")
    public R delete(@RequestBody Long[] ids){
        btNavBarService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }*/

    //分类信息
    @Autowired
    private BtCategroyManageService btCategroyManageService;
    @RequestMapping("/getmaincatagroy")
    public R getmaincatagroy(){
     List<BtCategroyManageEntity> maincatagroy =  btCategroyManageService.selectList(new EntityWrapper<BtCategroyManageEntity>().eq("pid",0L));
        return R.ok().put("maincatagroy",maincatagroy);
    }
    @RequestMapping("/getsubcataandchild")
    public R getsubcataandchild( Long pid,String cname,Boolean findsub){
        BtCategroyManageEntity sub = btCategroyManageService.selectOne(new EntityWrapper<BtCategroyManageEntity>().eq("pid",pid).like("name",cname));
        if(sub==null){
            return R.error(cname+"没有该分类");
        }
       Map<String,Object>backdata = btCategroyManageService.getfenlei(pid);
        return R.ok(backdata);
    }
}
